﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using ExamSystem.Mdeol;
namespace ExamSystem.DAL
{
    public class TckechengDAL
    {
        /// <summary>
        /// 添加课程
        /// </summary>
        public int inserttckecheng(string tkname)
        {
            string sql = "insert into tkecheng values('"+tkname+"')";
            return DBhepter.ExecuteNonQuery(sql);
        }


        /// <summary>
        /// 根据名称查询是否有数据
        /// </summary>
        public int selecttkcname(string tkcname)
        {
            string sql = "select * from tkecheng where tkcname='" + tkcname + "'";
            DataSet ds = DBhepter.GetDataSet(sql);
            DataTable dt = ds.Tables[0];
            return dt.Rows.Count;
        }

        /// <summary>
        /// 查询课程信息
        /// </summary>
        public List<Tkecheng> selectkckecheng()
        {
            List<Tkecheng> list = new List<Tkecheng>();
            string sql = "select * from tkecheng";
            DataSet ds = DBhepter.GetDataSet(sql);
            DataTable dt = ds.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                int tkcId = int.Parse(dr["tkcId"].ToString());
                string tkcname = dr["tkcname"].ToString();
                Tkecheng ts = new Tkecheng(tkcId, tkcname);
                list.Add(ts);
            }
            return list;
        }


        /// <summary>
        /// 根据课程编号删除数据
        /// </summary>
        public int delete(int tkcid)
        {

            string sql = "delete from tkecheng where tkcId=" + tkcid;
            return DAL.DBhepter.ExecuteNonQuery(sql);
        }



        /// <summary>
        /// 根据课程编号查询是否有数据
        /// </summary>
        public Tkecheng selecttkcid(int tcid)
        {
            Tkecheng t = null;
            string sql = "select * from tkecheng where tkcid=" + tcid + "";
            DataSet ds = DBhepter.GetDataSet(sql);
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                int tcId = (int)dr["tkcid"];
                string tcno = dr["tkcname"].ToString();
                t = new Tkecheng(tcId, tcno);
            }
            return t;
        }



        /// <summary>
        /// 根据课程编号修改数据
        /// </summary>
        public int update(Tkecheng t)
        {

            string sql = "update tkecheng set TkcName='" + t.TkcName + "' where tkcId=" + t.TkcId;
            return DAL.DBhepter.ExecuteNonQuery(sql);
        }
    }
}